package org.dromara.run.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.run.domain.bo.ScoreConfigBo;
import org.dromara.run.domain.vo.ScoreConfigVo;
import org.dromara.run.domain.ScoreConfig;
import org.dromara.run.mapper.ScoreConfigMapper;
import org.dromara.run.service.IScoreConfigService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 阳光跑成绩配置Service业务层处理
 *
 * @author chl
 * @date 2024-08-30
 */
@RequiredArgsConstructor
@Service
public class ScoreConfigServiceImpl implements IScoreConfigService {

    private final ScoreConfigMapper baseMapper;

    /**
     * 查询阳光跑成绩配置
     */
    @Override
    public ScoreConfigVo queryById(Long scoreConfigId){
        return baseMapper.selectVoById(scoreConfigId);
    }

    /**
     * 查询阳光跑成绩配置列表
     */
    @Override
    public TableDataInfo<ScoreConfigVo> queryPageList(ScoreConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ScoreConfig> lqw = buildQueryWrapper(bo);
        Page<ScoreConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询阳光跑成绩配置列表
     */
    @Override
    public List<ScoreConfigVo> queryList(ScoreConfigBo bo) {
        LambdaQueryWrapper<ScoreConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ScoreConfig> buildQueryWrapper(ScoreConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ScoreConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getScoreConfigName()), ScoreConfig::getScoreConfigName, bo.getScoreConfigName());
        lqw.eq(bo.getScoreConfigId()!=null, ScoreConfig::getScoreConfigId, bo.getScoreConfigId());
        return lqw;
    }

    /**
     * 新增阳光跑成绩配置
     */
    @Override
    public Boolean insertByBo(ScoreConfigBo bo) {
        ScoreConfig add = MapstructUtils.convert(bo, ScoreConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setScoreConfigId(add.getScoreConfigId());
        }
        return flag;
    }

    /**
     * 修改阳光跑成绩配置
     */
    @Override
    public Boolean updateByBo(ScoreConfigBo bo) {
        ScoreConfig update = MapstructUtils.convert(bo, ScoreConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ScoreConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除阳光跑成绩配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
